package com.azxcrm.project.monitor.workflow.service;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.project.monitor.workflow.common.CommonService;
import com.azxcrm.project.monitor.workflow.dao.WorkFlowApplyDao;
import com.azxcrm.project.monitor.workflow.domain.WorkFlowApply;

/**
 * 审批申请 服务层实现
 * 
 * @author Dustyone
 * @date 2018-08-07
 */
@Service
public class WorkFlowApplyServiceImpl implements WorkFlowApplyService {
	@Autowired
	private WorkFlowApplyDao workFlowApplyDao;

	@Autowired
	private CommonService commonService;

	@Autowired
	private RuntimeService runtimeService;

	/**
	 * 查询审批申请信息
	 * 
	 * @param id
	 *            审批申请ID
	 * @return 审批申请信息
	 */
	@Override
	/**
     * 查询审批申请信息
     * 
     * @param id 订单ID
     * @return 审批申请信息
     */
	public WorkFlowApply selectWorkFlowApplyOrderId(Integer orderId) {
		return workFlowApplyDao.selectWorkFlowApplyOrderId(orderId);
	}
	
	/**
	 * 查询审批申请信息
	 * 
	 * @param id
	 *            审批申请ID
	 * @return 审批申请信息
	 */
	@Override
	public WorkFlowApply selectWorkFlowApplyById(Integer id) {
		return workFlowApplyDao.selectWorkFlowApplyById(id);
	}

	/**
	 * 查询审批申请列表
	 * 
	 * @param workFlowApply
	 *            审批申请信息
	 * @return 审批申请集合
	 */
	@Override
	public List<WorkFlowApply> selectWorkFlowApplyList(WorkFlowApply workFlowApply) {
		return workFlowApplyDao.selectWorkFlowApplyList(workFlowApply);
	}

	/**
	 * 新增审批申请
	 * 
	 * @param workFlowApply
	 *            审批申请信息
	 * @return 结果
	 */
	@Override
	public int insertWorkFlowApply(WorkFlowApply workFlowApply) {
		return workFlowApplyDao.insertWorkFlowApply(workFlowApply);
	}

	/**
	 * 修改审批申请
	 * 
	 * @param workFlowApply
	 *            审批申请信息
	 * @return 结果
	 */
	@Override
	public int updateWorkFlowApply(WorkFlowApply workFlowApply) {
		return workFlowApplyDao.updateWorkFlowApply(workFlowApply);
	}

	/**
	 * 保存审批申请
	 * 
	 * @param workFlowApply
	 *            审批申请信息
	 * @return 结果
	 */
	@Override
	public int saveWorkFlowApply(WorkFlowApply workFlowApply) {
		Integer id = workFlowApply.getApplyId();
		int rows = 0;
		if (StringUtils.isNotNull(id)) {
			rows = workFlowApplyDao.updateWorkFlowApply(workFlowApply);
		} else {
			rows = workFlowApplyDao.insertWorkFlowApply(workFlowApply);
		}
		return rows;
	}

	/**
	 * 删除审批申请信息
	 * 
	 * @param id
	 *            审批申请ID
	 * @return 结果
	 */
	@Override
	public int deleteWorkFlowApplyById(Integer id) {
		return workFlowApplyDao.deleteWorkFlowApplyById(id);
	}

	/**
	 * 批量删除审批申请对象
	 * 
	 * @param ids
	 *            需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int batchDeleteWorkFlowApply(Integer[] ids) {
		return workFlowApplyDao.batchDeleteWorkFlowApply(ids);
	}

	/**
	 * 根据businessId查询审批信息
	 */
	@Override
	public WorkFlowApply selectWorkFlowApplyByBusinessId(String businessId) {

		return workFlowApplyDao.selectWorkFlowApplyByBusinessId(businessId);
	}
	
	
	@Override
	/**
     * 根据businessIds集合查询WorkFlowApply信息
     * @param 
     * @return 
     */
	public Integer[]  selectWorkFlowApplyByBusinessIds(List<String> businessIds) {
		return workFlowApplyDao.selectWorkFlowApplyByBusinessIds(businessIds);
	}

	/**
	 * 根据BizKey 查询相应的ProcessInstance信息
	 */
	@Override
	public String selectProcessInstanceIdByBizKey(String businessKey) {
		return workFlowApplyDao.selectProcessInstanceIdByBizKey(businessKey);
	}
	
	/**
	 * 根据userId获取Assignment Task Info
	 * @param userId
	 * @return
	 */
	public List<WorkFlowApply> getAssignmentTaskByUserId(String userId) {
		ArrayList<WorkFlowApply> flowInfoList = new ArrayList<>();
		List<Task> list = commonService.findTaskByUserId(userId);
		for (Task task : list) {
			ProcessInstance result = runtimeService.createProcessInstanceQuery()
					.processInstanceId(task.getProcessInstanceId()).singleResult();
			// 获得业务流程的bussinessKey
			String businessKey = result.getBusinessKey();
			WorkFlowApply flowInfo = workFlowApplyDao.selectWorkFlowApplyByBusinessId(businessKey);
			flowInfo.setTaskId(task.getId());
			flowInfoList.add(flowInfo);
		}

		return flowInfoList;

	}

	/**
	 * 根据applyId查询审批后的详情信息
	 * @param applyId
	 * @return
	 */
	@Override
	public WorkFlowApply selectWorkFlowApplyByApplyId(Integer applyId) {
		// TODO Auto-generated method stub
		return workFlowApplyDao.selectWorkFlowApplyByApplyId(applyId);
	}
}
